package com.lightsh.sensor;

import java.util.Arrays;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;

public class AccelerometerSensor extends BaseSensor{	
	private static final float DELTA_ACCELE_MOVE = 1.5f;
	private float[] mValues = {0f, 0f, 0f}; 
	private boolean mInited = false;
	
	public AccelerometerSensor(Context context){
		super(context);
	}
	
	@Override
	protected int getSensorType() {
		return Sensor.TYPE_ACCELEROMETER;
	}
	@Override
	protected String getLogTag() {
		return "AcceleSensorController";
	}
	@Override
	protected void onSensorChanged(SensorEvent event) {
		sensorChanged(event);
	}
	
	private void sensorChanged(SensorEvent event){
		float x = event.values[0];
		float y = event.values[1];
		float z = event.values[2];
		
		boolean isMove = isMove(x, y, z);
		if(isMove && mInited){
			onMove(event.values);
		}
		mValues[0] = x;
		mValues[1] = y;
		mValues[2] = z;
		mInited = true;
	}
	
	private boolean isMove(float newX, float newY, float newZ){
//		Log.i(TAG, "sensor change : " + Arrays.toString(mValues) + " -> [" + newX + "," + newY + "," + newZ + "]");
		float deltaX = Math.abs(mValues[0] - newX);
		float deltaY = Math.abs(mValues[1] - newY);
		float deltaZ = Math.abs(mValues[2] - newZ);
		if(deltaX > DELTA_ACCELE_MOVE || deltaY > DELTA_ACCELE_MOVE || deltaZ > DELTA_ACCELE_MOVE){
			logD("delta value : " + deltaX + " , " + deltaY + " , " + deltaZ);
			return true;
		}
		return false;
	}
	
	private void onMove(float[] values){
		VibratorHelper.vibrate(mContext, 100);
		logD("Phone move : " + Arrays.toString(mValues) + " -> " + Arrays.toString(values));
	}
}
